#include "mysql.h"

#include <QDir>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>

MySql::MySql(QString& Dir)
    : dbDir(Dir)
{
    connectionName = "sqlslite";
    if (!isDirExist())        //若路径不存在则创建该路径//
    {
        QDir backup;
        backup.mkdir(dbDir);
    }

    if (!CreateConnection())
    {
        qDebug() << "can not open db!";
        return;
    }
}

MySql::~MySql()
{
    closeDb();
}


bool MySql::CreateConnection()
{
    db = QSqlDatabase::addDatabase("QSQLITE",connectionName);  //创建一个SQLite数据库连接//
    QString dbName = dbDir + "XY.db";                         //本地数据库文件名//
    db.setDatabaseName(dbName);                                //数据库连接命名//
    if (!db.open())                                            //如果.db文件不存在，自动新建.db文件并打开//
    {
        return false;
    }

    QSqlQuery query(QSqlDatabase::database(connectionName));
    query.exec("create table UserInfo(ID int auto_increment primary key,UserName varchar(50) NOT NULL,UserAge varchar(50),UserSex varchar(50))");
    query.exec("create table DesignDataInfo(ID int auto_increment primary key,name varchar(50) NOT NULL,LeverNum varchar(20),content varchar(500000))");
    return true;
}


bool MySql::isDirExist()
{
    QDir temp_dir(dbDir);
    return temp_dir.exists();
}

void MySql::closeDb()
{
    if (db.isOpen())
    {
        db.close();
    }
}

bool MySql::UserInsert(UserInfoBean& userBean)
{
    requestAccessToken();
    QSqlQuery query(QSqlDatabase::database(connectionName));
    query.prepare("insert into UserInfo(UserName,UserAge,UserSex)"
                  "values(:UserName,:UserAge,:UserSex)");
    query.bindValue(":UserName",userBean.UserName);
    query.bindValue(":UserAge",userBean.UserAge);
    query.bindValue(":UserSex",userBean.UserSex);
    if (!query.exec())
    {
        qDebug() << "/*************/" << "false" ;
        return false;
    }

    releaseAccessToken();
    qDebug() << "/*************/" << "Ok" ;
    return true;
}
bool MySql::DesignDataInsert(designDataBean& dataBean)
{
    requestAccessToken();

    QSqlQuery query(QSqlDatabase::database(connectionName));
    query.prepare("insert into DesignDataInfo(name,LeverNum,content)"
        "values(:name,:LeverNum,:content)");
    query.bindValue(":name",dataBean.name);
    query.bindValue(":LeverNum",dataBean.LeverNum);
    query.bindValue(":content",dataBean.content);
    if (!query.exec())
    {
        qDebug() << "/*************/" << "false" ;
        return false;
    }

    releaseAccessToken();
    qDebug() << "/*************/" << "Ok" ;
    return true;


}

bool MySql::DesignDataSelect(QString name,designDataBean& dataBean)
{
    requestAccessToken();
    QSqlQuery query(QSqlDatabase::database(connectionName));
    query.prepare("select * from DesignDataInfo where name = :name");
    query.bindValue(":name",name);
    if (!query.exec())
    {
        return false;
    }
    query.next();
    dataBean.name = query.value(1).toString();
    dataBean.LeverNum = query.value(2).toInt();
    dataBean.content = query.value(3).toString();
    releaseAccessToken();
    return true;
}

bool MySql::DesignDataQueryBy(QString name)
{
    requestAccessToken();
    bool b=false;
    QSqlQuery query(QSqlDatabase::database(connectionName));
    query.prepare("select * from DesignDataInfo where name = :name");
    query.bindValue(":name",name);
    if (query.exec())
    {
        if(query.next())
            b=true;
    }
    releaseAccessToken();
    return b;
}

bool MySql::DesignDataDelete(QString name)
{
    requestAccessToken();
    QSqlQuery query(QSqlDatabase::database(connectionName));
    query.prepare("delete from DesignDataInfo where name = :name");
    query.bindValue(":name",name);

    if (!query.exec())
    {
        return false;
    }

    releaseAccessToken();
    return true;
}



void  MySql::requestAccessToken(void)
{
    mutex.lock();                           //如果已经lock且还未unlock，一直等待，直到unlock，然后lock//
}

void  MySql::releaseAccessToken(void)
{
    mutex.unlock();
}
QList<designDataBean> MySql::QueryAllDesignData()
{
    requestAccessToken();
    QList<designDataBean>uibs;
    designDataBean dataBean;
    QSqlQuery query(QSqlDatabase::database(connectionName));
    query.prepare("select * from DesignDataInfo order by ID desc");
    if(query.exec()){
        while(query.next()){
            dataBean.name = query.value(1).toString();
            dataBean.LeverNum = query.value(2).toInt();
            dataBean.content = query.value(3).toString();
            uibs.append(dataBean);
        }
    }
    releaseAccessToken();
    return uibs;
}



